package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.OpenedState;
import ssm.dao.TreePojo;
import ssm.model.DeviceMapper;
import ssm.model.DeviceTreeMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DeviceServiceImpl implements  DeviceService{

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceTreeMapper deviceTreeMapper;

    public Map<String, Object> getTree() {
        Map<String,Object>result=new HashMap<String,Object>();
        //处理监控设备
        List<TreePojo> camers=deviceTreeMapper.findTrunk(1);
        List<TreePojo> camers_leaf=deviceMapper.findLeaf(1);
        camers.addAll(camers_leaf);
        camers=addState(camers,false);
        result.put("jiankong",camers);
        //处理一卡通设备
        List<TreePojo> cards=deviceTreeMapper.findTrunk(2);
        List<TreePojo> cards_leaf=deviceMapper.findLeaf(2);
        cards.addAll(cards_leaf);
        cards=addState(cards,false);
        result.put("ykt",cards);
        //处理人脸设备
        List<TreePojo> faces=deviceTreeMapper.findTrunk(3);
//        System.out.println("faces:"+faces);
        List<TreePojo> faces_leaf=deviceMapper.findLeaf(3);
        faces.addAll(faces_leaf);
        faces=addState(faces,false);
        for(TreePojo t:faces){
            System.out.println(t);
        }
        result.put("renlian",faces);
        //查询车辆卡口设备
        List<TreePojo> cars=deviceTreeMapper.findTrunk(4);
        List<TreePojo> cars_leaf=deviceMapper.findLeaf(4);
        cars.addAll(cars_leaf);
        cars=addState(cars,false);
        result.put("kakou",cars);
        //查询wifi设备
        List<TreePojo> wifis=deviceTreeMapper.findTrunk(5);
        List<TreePojo> wifis_leaf=deviceMapper.findLeaf(5);
        wifis.addAll(wifis_leaf);
        wifis=addState(wifis,false);
        result.put("wifi",wifis);
        return result;
    }



    public List<TreePojo> addState(List<TreePojo> trees,boolean statr){
        for(TreePojo treePojo:trees){
            treePojo.setState(new OpenedState(statr));
        }
        return trees;
    }
}
